<template>
    <el-drawer ref="FolderDetail" append-to-body :wrapperClosable="false" show-close :title="folderEidtorType == 'newFolder' ? '新建文件夹' : '更新文件夹' " direction="ltr" size="400px" :before-close="handleClose" :visible.sync="folderEidtorStatus">
        <div class="folderDetail">
            <el-form :model="folderDetail" :rules="rules" ref="ruleForm" class="FolderDetail">
                <el-form-item prop="title">
                    <el-input class="textStyle" v-model="folderDetail.title" placeholder="文件夹名称"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-input class="textStyle" type="textarea" resize="none" maxlength="1000" show-word-limit placeholder="文件夹备注" v-model="folderDetail.description" :autosize="{ minRows: 20, maxRows: 20}">
                    </el-input>
                </el-form-item>
                <el-form-item style="text-align: right;">
                    <el-button type="primary" @click="submit">提交</el-button>
                </el-form-item>
            </el-form>
        </div>
    </el-drawer>
</template>

<script>
import { mapState } from 'vuex'

export default {
    data() {
        return {
            folderDetail: {
                title: '',
                description: '',
            },
            rules: {
                title: [
                    { required: true, message: '请输入文件夹名称', trigger: 'blur' },
                ],
            },
        }
    },
    watch: {
        folderEidtorStatus: function (newVal, oldVal) {
            if (newVal) {
                if (this.folderEidtorType == 'newFolder') {
                    this.folderDetail = {
                        title: '',
                        description: '',
                    };
                } else if (this.folderEidtorType == 'updateFolder') {
                    this.folderDetail = {
                        title: this.folderEidtorObject.title,
                        description: this.folderEidtorObject.description,
                    };
                };
            };
        },
    },
    computed: {
        ...mapState("doc_tree", {
            folderEidtorStatus: state => state.folderEidtor.status,
            folderEidtorType: state => state.folderEidtor.type,
            folderEidtorObject: state => state.folderEidtor.object,
        }),
    },
    methods: {
        handleClose() {
            this.$store.dispatch('doc_tree/clearFolderEidt');
        },
        submit() {
            var confirm = this.$confirm(this.folderEidtorType == 'newFolder' ? '是否创建文件夹？' : '是否更新文件夹？');
            if (confirm) {
                this.$store.dispatch('doc_tree/editFolder', {
                    title: this.folderDetail.title,
                    description: this.folderDetail.description,
                });
            };
        }
    },
}
</script>

<style scoped>
    .FolderDetail {
        width: 90%;
        margin: 20px auto;
    }
</style>